package org.dicr.util.jds.conn.gui;

import javax.swing.*;

import org.dicr.event.*;
import org.dicr.util.jds.conn.*;

/***********************************************************************************************************************
 * Список соединений.
 * 
 * @author Igor A Tarasov me@dicr.dn.ua
 * @version 030103
 **********************************************************************************************************************/
public class ConnectionsList extends JList {
	private static final long serialVersionUID = 354013359673062102L;

	private ConnectionsFactory factory = null;

	private ConnectionsListModel model = new ConnectionsListModel();

	private ConnectionsListCellRenderer renderer = new ConnectionsListCellRenderer();

	/*******************************************************************************************************************
	 * Конструктор
	 ******************************************************************************************************************/
	public ConnectionsList() {
		super();
		this.setModel(this.model);
		this.setCellRenderer(this.renderer);
	}

	/*******************************************************************************************************************
	 * Установка менеджера соединений
	 * 
	 * @param newFactory connection factory
	 ******************************************************************************************************************/
	public void setFactory(ConnectionsFactory newFactory) {
		if (this.factory != newFactory) {
			this.factory = newFactory;
			this.model.setFactory(this.factory);
			this.renderer.setFactory(this.factory);
			try {
				if (this.factory != null && this.factory.getDefaultConnection() != null) setSelectedValue(
				        this.factory.getDefaultConnection(), true);
				else clearSelection();
			} catch (Exception ex) {
				DicrExceptionHandler.handleException(ex, this);
			}
		}
	}
}
